angular.module("hyt.workbenchCtrl",[])

  .controller('workbenchCtrl', function(Storage,$scope,$ionicPopover,FirstRecord,$state) {
    var loginMsg=Storage.get('user');
    var lat=Storage.get('lat');
    var long=Storage.get('long');
    var point = new BMap.Point(long,lat);
    var geoc = new BMap.Geocoder();
    geoc.getLocation(point, function(rs){
      var addComp = rs.addressComponents;
      Storage.set('gpsProvince',addComp.province);
      Storage.set('gpsCity',addComp.city);
      Storage.set('gpsArea',addComp.district);
    });
    Storage.set('receiveAddress',null);
    Storage.set('shippingAddress',null);
    Storage.set('addOrder',null);
    $scope.showloading=true;
    var firstRecord={
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    FirstRecord.getFirstRecord(JSON.stringify(firstRecord));
    $scope.$on('FirstRecord.Updated', function() {
      $scope.fristProcess=FirstRecord.getResult().rows;
      $scope.showloading=false;
    });
    $scope.recordClick=function(id){
      Storage.set('orderId',id);
      $state.go('detailTab.process',{htmlId:2});
    }
  })
  .controller('workbenchSearchCtrl',function($scope,Storage,Orders,$state,$ionicPopup,RevokeOrder,$ionicLoading,SubmitOrder){
    $scope.showloading=false;
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    $scope.search={
      parameter:{
        shipperId:userMsg.rows.id,
        orderNo:''
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.$on('$ionicView.enter', function() {
      if ($scope.search.parameter.orderNo==''){
        $("#searchOd").focus();
      }
    });
    document.onkeydown=function(event){
      if(event.keyCode==13&&$scope.search.parameter.orderNo!=''){
        $scope.showloading=true;
        Orders.getOrderData($scope.search);
      }else if (event.keyCode==13&&$scope.search.parameter.orderNo==''){
        $scope.orderData='';
      }
    };
    $scope.$on('OrderList.Updated', function() {
      $scope.orderData=Orders.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      if ($scope.search.parameter.orderNo!=''){
        Orders.getOrderData($scope.search);
      }
      $scope.$broadcast('scroll.refreshComplete');
    };
    $scope.orderRevoke=function(id){
      var revokeOrder={
        id:id,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      var confirmPopup = $ionicPopup.confirm({
        title:'是否撤销订单',
        okText:'是',
        cancelText:'否'
      });
      confirmPopup.then(function(res) {
        if(res){
          RevokeOrder.getRevokeOrder(JSON.stringify(revokeOrder));
          $scope.$on('RevokeOrder.Updated', function() {
            var result=RevokeOrder.getResult();
            if (result.success==true){
              $ionicLoading.show({
                noBackdrop: true,
                template: result.rows,
                duration: 1500
              });
              Orders.getOrderData($scope.search);
            }
          });
        }
        else {
          return false;
        }
      });
    };
    $scope.orderSubmit=function(id){
      var submitOrder={
        id:id,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      var confirmPopup = $ionicPopup.confirm({
        title:'是否提交订单',
        okText:'是',
        cancelText:'否'
      });
      confirmPopup.then(function(res) {
        if(res){
          SubmitOrder.getSubmitOrder(JSON.stringify(submitOrder));
          $scope.$on('SubmitOrder.Updated', function() {
            var result=SubmitOrder.getResult();
            if (result.success==true){
              $ionicLoading.show({
                noBackdrop: true,
                template: result.rows,
                duration: 1500
              });
              Orders.getOrderData($scope.search);
            }
          });
        }
        else {
          return false;
        }
      });
    };
    $scope.goDetail=function(id){
      Storage.set('orderId',id);
      $state.go('detailTab.detail');
    };
    $scope.changeOrder=function(id){
      $state.go('changeOrder',{id:id});
    }
  })
  .controller('noticeListCtrl',function($scope,Storage,NoticeList,$stateParams){
    $scope.showloading=true;
    var loginMsg=Storage.get('user');
    var noticeSearch={
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    NoticeList.getNoticeData(noticeSearch);
    $scope.$on('Notice.Updated', function() {
      $scope.noticeData=NoticeList.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      NoticeList.getNoticeData(noticeSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore=function(){
      NoticeList.getMoreNoticeData(noticeSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
      return NoticeList.hasNextPage();
    };
    if ($stateParams.title!=undefined||$stateParams.content!=undefined){
      $scope.notice={
        title:$stateParams.title,
        content:$stateParams.content
      }
    }
  })
